﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//using Model;

/// <summary>
/// Description résumée de CreneauManager
/// </summary>
/// 
namespace DAL
{
    public class CreneauManager : ICreneauManager
    {
        private IAATOEntities dbService;

        public CreneauManager()
        {
            dbService = new IAATOEntities();
        }


        public bool addCreneau(CRENEAU creneau)
        {
            try
            {
                dbService.CRENEAUJeu.AddObject(creneau);
                dbService.SaveChanges();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool deleteCreneau(int creneauId)
        {
            try
            {
                CRENEAU creneau = (from c in dbService.CRENEAUJeu
                             where c.ID == creneauId
                             select c).First();

                dbService.DeleteObject(creneau);
                dbService.SaveChanges();
                return true;
            }

            catch (Exception ex)
            {
                return false;
            }

        }

        public bool updateCreneau()
        {
            try
            {
                dbService.SaveChanges();
                return true;
            }

            catch (Exception ex)
            {
                return false;
            }
        }

        public CRENEAU getCreneauById(int id)
        {
            try
            {
                return (from c in dbService.CRENEAUJeu
                        where c.ID == id
                        select c).First();
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public List<CRENEAU> getAllCreneaux()
        {
            try
            {
                return (from c in dbService.CRENEAUJeu
                        orderby c.ORDRE
                        select c).ToList();
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public CRENEAU getCreneauForProgrammeAndOrder(int programmeId, int ordre)
        {
            try
            {
                return (from c in dbService.CRENEAUJeu
                        where c.PROGRAMMEID == programmeId && c.ORDRE == ordre
                        select c).First();
            }
            catch (Exception ex)
            {
                return null;
            }
        }



        public List<CRENEAU> getCreneauxAtDateAndOrder(DateTime date, int ordre)
        {
            try
            {
                return (from c in dbService.CRENEAUJeu
                        where c.PROGRAMME.DATE == date && c.ORDRE == ordre
                        select c).ToList();
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public List<CRENEAU> getCreneauxAtDate(DateTime date)
        {
            try
            {
                return (from c in dbService.CRENEAUJeu
                        where c.PROGRAMME.DATE == date
                        select c).ToList();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}